package com.xlro.servlet;

import com.xlro.service.RegisterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet" , urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {

    private RegisterService registerService = new RegisterService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String reader = req.getParameter("reader");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String againPassword = req.getParameter("againPassword");

        String title = null,result =null;

        if(password.equals(againPassword)) {
            String s = registerService.insertOne(reader, username, password);
            if ("注册成功".equals(s)) {
                result = "注册成功";
                title = "注册成功";
                req.getRequestDispatcher("./index.jsp?title=" + title + "&message=" + result).forward(req, resp);
            }else {
                title = "注册失败";
                result = "重新输入！";
                req.getRequestDispatcher("./register.jsp?title=" + title + "&message=" + result).forward(req, resp);
            }
        }else {
            title = "注册失败";
            result = "密码不一致";
            req.getRequestDispatcher("./register.jsp?title=" + title + "&message=" + result).forward(req, resp);
        }

    }
}
